//
//  FileImg.m
//  MoodDiary
//
//  Created by OVIX on 2021/1/21.
//

#import "FileImg.h"

@implementation FileImg

+ (NSString *)getImagePath:(UIImage *)image {
    return [[self alloc] getImagePath:image];
}

- (NSString *)getImagePath:(UIImage *)image {
    
    NSString *filePath = nil;
    NSData *data = UIImagePNGRepresentation(image) == nil ? UIImageJPEGRepresentation(image, 0.5) : UIImagePNGRepresentation(image);
    
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
    NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    //把刚刚图片转换的data对象拷贝至沙盒中
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *imagePath = [NSString stringWithFormat:@"/%@.png", [NSString currentTimeStamp]];
    [[NSFileManager defaultManager] createFileAtPath:[documentsPath stringByAppendingString:imagePath] contents:data attributes:nil];
    
    //得到选择后沙盒中图片的完整路径
    filePath = [[NSString alloc]initWithFormat:@"%@%@",documentsPath,imagePath];
    
    return filePath;
}

@end
